package com.zackeus.common.utils.media;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.media.ToneGenerator;
import android.os.Build;
import android.os.Vibrator;

import com.zackeus.common.utils.ObjectUtils;
import com.zackeus.common.utils.Utils;

/**
 * 媒体管理器
 * @name: MediaManager
 * @author: zz
 * @date: 2022/10/13
 */
public class MediaManager {

    private static volatile MediaManager mInstance;

    private static final long[] VIBRATOR_PATTERN = {0, 1000, 1000};
    private final AudioManager audioManager;
    private final AudioAttributes audioAttributes;
    private final Vibrator mVibrator;
    private final Ringtone mRingTone;

    private final ToneGenerator toneGenerator;

    private MediaManager() {
        this.audioManager = (AudioManager) Utils.getContext().getSystemService(Context.AUDIO_SERVICE);
        this.mVibrator = (Vibrator) Utils.getContext().getSystemService(Context.VIBRATOR_SERVICE);
        this.mRingTone = RingtoneManager.getRingtone(Utils.getContext(),
                RingtoneManager.getActualDefaultRingtoneUri(Utils.getContext(), RingtoneManager.TYPE_RINGTONE));
        audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .build();

        this.toneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, 100);
    }

    public static MediaManager getInstance() {
        if (mInstance == null) {
            synchronized (MediaManager.class) {
                if (mInstance == null) {
                    mInstance = new MediaManager();
                }
            }
        }
        return mInstance;
    }

    /**
     * 设置免提
     * @param isActive
     */
    public synchronized void setSpeakerMode(final boolean isActive) {
        audioManager.setMode(AudioManager.MODE_NORMAL);
        audioManager.setSpeakerphoneOn(isActive);
    }

    /**
     * 播放振铃音
     * @param vibrate 是否震动
     */
    public synchronized void startRingTone(boolean vibrate) {
        this.stopRingTone();
        if (ObjectUtils.isNull(this.audioManager))
            return;

        if (AudioManager.RINGER_MODE_SILENT == audioManager.getRingerMode())
            /* 静音模式 不处理 */
            return;

        if (AudioManager.RINGER_MODE_VIBRATE == audioManager.getRingerMode() && vibrate) {
            /* 震动模式 */
            mVibrator.vibrate(VIBRATOR_PATTERN, 0, audioAttributes);
            return;
        }

        if (AudioManager.RINGER_MODE_NORMAL == audioManager.getRingerMode()) {
            /* 普通模式 */
            if (vibrate) {
                mVibrator.vibrate(VIBRATOR_PATTERN, 0, audioAttributes);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                /* 循环播放 */
                mRingTone.setLooping(Boolean.TRUE);
            }
            mRingTone.play();
        }
    }

    /**
     * 播放回铃音
     */
    public synchronized void startRingBackTone() {
        this.stopRingBackTone();
        toneGenerator.startTone(ToneGenerator.TONE_SUP_RINGTONE);
    }

    /**
     * 停放振铃音
     */
    public synchronized void stopRingTone() {
        mVibrator.cancel();
        mRingTone.stop();
    }

    /**
     * 停放回铃音
     */
    public synchronized void stopRingBackTone() {
        toneGenerator.stopTone();
    }

    public void stopAll() {
        this.stopRingTone();
        this.stopRingBackTone();
    }
}
